package com.ziq.zcamp.constant

import android.net.Uri
import android.text.TextUtils
import com.ziq.zcamp.manager.UserManager
import java.io.UnsupportedEncodingException
import java.net.URLEncoder

enum class H5Url {

    /**
     * 营地上传
     */
    CAMP_UPLOAD("H5_APP_HOST/#/app/campUpload"),

    /**
     * 帖子详情
     */
    POSTING_DETAIL("H5_APP_HOST/#/subpages/community/post-detail/index/"),
    /**
     * 视频详情
     */
    POST_VIDEO_DETAIL("H5_APP_HOST/#/subpages/community/video-detail/index/"),
    /**
     * 营地详情
     */
    PLACE_DETAIL("H5_APP_HOST/#/subpages/camp/camp-detail/index/"),
    /**
     * 营地商品详情
     */
    PLACE_PRODUCT_DETAIL("H5_APP_HOST/#subpages/camp/commodity-detail/index/"),
    /**
     * 活动管理
     */
    ACTIVITY_DETAILS("H5_APP_HOST/#/subpages/community/activity-detail/index"),
    /**
     * 话题详情
     */
    TOPICS_DETAILS("H5_APP_HOST/#/subpages/community/topic-detail/index"),

    /**
     * 领取优惠券
     */
    RECEIVE_COUPON("H5_APP_HOST/#/subpages/coupon/get-coupon/index"),

    /**
     * 商家券推广领劵页
     */
    COUPON_SHARE("H5_APP_HOST/#/subpages/other/web-view/index?url=");


    var url:String? = null

    fun getUrlWithAtom():String{
        return getUrlStr(url?:"", UserManager.instance.getAtomMap())
    }

    constructor(url: String){
        this.url = url
    }

    private fun getUrlStr(url: String, vararg paramsMapArray: Map<String, Any?>?): String {
        if (TextUtils.isEmpty(url)) {
            throw IllegalAccessError("URL为空")
        }
        val urlBuffer = StringBuffer()
        if (Uri.parse(url).queryParameterNames.size != 0) {
            urlBuffer.append("$url&")
        } else {
            if (url[url.length - 1] != '?') {
                urlBuffer.append("$url?")
            }
        }
        for (paramsMap in paramsMapArray) {
            if (paramsMap != null && paramsMap.isNotEmpty()) {
                val entryIterator = paramsMap.entries.iterator()
                while (entryIterator.hasNext()) {
                    val entry = entryIterator.next()
                    if (null == entry.value) {
                        continue
                    }
                    val k = entry.key
                    val v = entry.value.toString()
                    val index = urlBuffer.lastIndexOf("?")
                    if (index != urlBuffer.length - 1) {
                        urlBuffer.append("&")
                    }
                    try {
                        urlBuffer.append(k + "=" + URLEncoder.encode(v, "UTF-8"))
                    } catch (e: UnsupportedEncodingException) {
                        e.printStackTrace()
                    }
                }
            }
        }
        return urlBuffer.toString()
    }

}